﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Zhaoxi.Test
{
	public class BinarySearchBatch
	{
		public static void Show()
		{
			List<string> line = new List<string>();
			StreamReader sr = new StreamReader("Sample.txt", System.Text.Encoding.UTF8);
			int i = 0;
			while (!sr.EndOfStream)
			{
				line.Add(sr.ReadLine());
				i++;
			}
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();

			for (int p = 0; p < line.Count - 1; p++)
			{

				int j, lowe = 0, high = ++i;

				var str = line[p].ToString().Split(' ')[0] + " ";
				while (lowe <= high)
				{
					j = (lowe + high) / 2;

					if (j >= line.Count)
					{
						break;
					}
					if (line[j].Length <= str.Length)
					{

						lowe = j + 1;
						continue;
					}
					string m = line[j].Remove(str.Length);
					int k = string.Compare(m, str);
					if (k == 0)
					{
						Console.WriteLine("{0}", line[j]);
						break;

					}
					if (k == 1)
						high = j - 1;
					else
						lowe = j + 1;

				}
				if (lowe > high)
				{
					Console.WriteLine("没有这个单词");
				}


			}
			stopwatch.Stop();
			Console.WriteLine($"用时：{stopwatch.Elapsed.TotalMilliseconds}");
		}


		public static void Show2()
		{
			List<string> line = new List<string>();

			StreamReader sr = new StreamReader("Sample.txt", System.Text.Encoding.UTF8);

			while (!sr.EndOfStream)
			{
				line.Add(sr.ReadLine());
			}
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();

			for (int i = 0; i < line.Count - 1; i++)
			{
				var str = line[i].ToString().Split(' ')[0] + " ";
				for (int j = 0; j < line.Count; j++)
				{
					if (line[j].Length <= str.Length)
					{
						continue;
					}
					string m = line[j].Remove(str.Length);
					int k = string.Compare(m, str);
					if (k == 0)
					{
						Console.WriteLine("{0}", line[j]);
						break;
					}
					else
					{
						continue;
					}
				}
			}

			stopwatch.Stop();
			Console.WriteLine($"用时：{stopwatch.Elapsed.TotalMilliseconds}");

		}
	}
}

